//
//  LiuEmoticonTool.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/4/7.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit

let NumberOfPage = 20

let filePath:String = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("recentArray.archive")


class LiuEmoticonTool: NSObject {

    //全局访点
    static var sharedEmoticonManager:LiuEmoticonTool = LiuEmoticonTool()
    
    fileprivate override init() {
        super.init()
        
        
    }
    
    //供外界访问的model的三维数组
    lazy var emoticonArray:[[[LiuEmoticonModel]]] = {
        
        return [
            [self.recentArray],
            self.seperatArray(array: self.defaultArray),
            
            self.seperatArray(array: self.emojiArray),
            
            self.seperatArray(array: self.lxhArray)
            
        ]
    }()
    
    //default图片
    fileprivate lazy var defaultArray:[LiuEmoticonModel] = {
        let array = self.loadImage(finderPath: "default", filePath: "info")
        return array
    }()
    
    fileprivate lazy var emojiArray:[LiuEmoticonModel] = {
        let array = self.loadImage(finderPath: "emoji", filePath: "info")
        return array
    }()
    
    fileprivate lazy var lxhArray:[LiuEmoticonModel] = {
        let array = self.loadImage(finderPath: "lxh", filePath: "info")
        return array
    }()
    
    //最近
    fileprivate lazy var recentArray:[LiuEmoticonModel] = {
        //从本地获取最近数据
        guard let temparray = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [LiuEmoticonModel] else {
            return [LiuEmoticonModel]()
        }

        return temparray
    }()
    
    //创建bundle对象
    fileprivate lazy var emoticonBundle:Bundle = {
        
        //获取bundle的bundle路径
        let bundlePath = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        
        //创建bundle对象
        let bundle = Bundle(path: bundlePath)!
        
        return bundle
    }()
    
    //解析图片
    fileprivate func loadImage (finderPath:String, filePath:String) -> [LiuEmoticonModel]{
        
        //获取plist文件路径
        let plistPath = emoticonBundle.path(forResource: finderPath + "/" + filePath + ".plist", ofType: nil)!
        
        //解析文件
        let dicArray:[[String:Any]] = NSArray(contentsOfFile: plistPath) as! [[String : Any]]
        let emoticonArray:[LiuEmoticonModel] = NSArray.yy_modelArray(with: LiuEmoticonModel.self, json: dicArray) as! [LiuEmoticonModel]
        
        //拼接图片的全路径
        for model in emoticonArray {
            if model.type == "0" {
                //是图片
                model.partPath = "/Contents/Resources/" + finderPath + "/" + model.png!
            }
            
            
        }
        
        return emoticonArray
    }
    
    //将图片20为一组分组
    fileprivate func seperatArray(array:[LiuEmoticonModel]) -> [[LiuEmoticonModel]] {
        
        //根据传入的表情确定组数
        let section = (array.count - 1)/NumberOfPage + 1
        
        //创建一个可变数组存放分组后的数组
        var newArray = [[LiuEmoticonModel]]()
        
        
        
        //遍历分组
        for i in 0..<section {
            //开始截取的位置
            let start = i * NumberOfPage
            var end = NumberOfPage
            
            //如果最后一组不够20,改变end
            if start + end > array.count {
                end = array.count - start
            }
            
            //截取的范围
            let range = NSMakeRange(start, end)
            
            //截取数组
            let partArray:[LiuEmoticonModel] = (array as NSArray).subarray(with: range) as! [LiuEmoticonModel]
            
            //将截取的数组放到数组中
            newArray.append(partArray)
            
        }
        
        return newArray
        
    }
    
    //最近一组的数组
    fileprivate func latelyArray() -> [[LiuEmoticonModel]] {
        
        let array = [LiuEmoticonModel]()
        
        return [array]
    }
    
    //保存最近的表情模型
    func saveRecentEmoticon(emoticon:LiuEmoticonModel) {
        //判断这个model是否在数组中
        for (i,rencentemoticon) in recentArray.enumerated() {
            if emoticon.type == "0" {
                //是图片表情
                if rencentemoticon.chs == emoticon.chs {
                    recentArray.remove(at: i)
                    return
                }
            } else {
                //是emoji表情
                if rencentemoticon.code == emoticon.code {
                    recentArray.remove(at: i)
                    return
                }
            }
        }
        
        //添加表情model到第一位
        recentArray.insert(emoticon, at: 0)
        
        //修改数组
        emoticonArray[0][0] = recentArray
        
        //归档
        
        
        NSKeyedArchiver.archiveRootObject(recentArray, toFile: filePath)
        
        print(filePath)
        
    }
}











